package com.ml.ws;

import java.io.IOException;
import java.io.InputStream;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.JdbcTemplate;

public class IMHelper {

	private static final Logger logger = Logger.getLogger(IMHelper.class);
	
	@Resource
	private static JdbcTemplate jdbcTemplate;
	
	private static final String CONFIG_PROP = "config.properties";
	private static final String CUST_NEW = "im_cust_new";
	private static final String CUST_UPD = "im_cust_upd";
	private static final String CUST_DEL = "im_cust_del";
	private static final String CUST_QRY = "im_cust_qry";
	private static final String CUST_REV = "im_cust_rev";
	private static Map<String, Object> spMap;
	
	static {
		// 加载配置文件,读取配置文件中的SP信息
		spMap = new HashMap<String, Object>();
		InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(CONFIG_PROP);
		Properties prop = new Properties();
		try {
			prop.load(in);
			
			if(prop.get(CUST_NEW) != null){
				spMap.put(CUST_NEW, prop.get(CUST_NEW));
			}
			if(prop.get(CUST_UPD) != null){
				spMap.put(CUST_UPD, prop.get(CUST_UPD));
			}
			if(prop.get(CUST_DEL) != null){
				spMap.put(CUST_DEL, prop.get(CUST_DEL));
			}
			if(prop.get(CUST_QRY) != null){
				spMap.put(CUST_QRY, prop.get(CUST_QRY));
			}
			if(prop.get(CUST_REV) != null){
				spMap.put(CUST_REV, prop.get(CUST_REV));
			}
			
		} catch (IOException e) {
			logger.warn("读取配置文件中的sp失败 : "+ e.getMessage());
			e.printStackTrace();
		}
	}
	
	/**
	 * 开户
	 * @param customer
	 * @return
	 */
	public static IMReport create(final IMCustomer customer){
		
		String sp = "{"+spMap.get(CUST_NEW)+"}";
		
		IMReport ir = jdbcTemplate.execute(sp, new CallableStatementCallback<IMReport>(){
			public IMReport doInCallableStatement(CallableStatement cs)
					throws SQLException, DataAccessException {
				
//				cs.setString(1, customer.getMemberID());
				
				cs.execute();
				
				ResultSet rs = cs.getResultSet();
				
				IMReport ir = new IMReport(); 
				
				while(rs.next()){
					ir.setO_status(rs.getString(1));
					ir.setO_retmsg(rs.getString(2));
					ir.setO_seqno(rs.getString(3));
				}
				
				return ir;
			}
		});
		
		return ir;
	}
	
	/**
	 * 修改会员信息
	 * @param customer
	 * @return
	 */
	public static IMReport modify(IMCustomer customer){
		String sp = "{"+spMap.get(CUST_UPD)+"}";
		
		IMReport ir = jdbcTemplate.execute(sp, new CallableStatementCallback<IMReport>(){
			public IMReport doInCallableStatement(CallableStatement cs)
					throws SQLException, DataAccessException {
				
//				cs.setString(1, customer.getMemberID());
				
				cs.execute();
				
				ResultSet rs = cs.getResultSet();
				
				IMReport ir = new IMReport(); 
				
				while(rs.next()){
					ir.setO_status(rs.getString(1));
					ir.setO_retmsg(rs.getString(2));
					ir.setO_seqno(rs.getString(3));
				}
				
				return ir;
			}
		});
		
		return ir;
	}
	
	/**
	 * 销户
	 * @param customer
	 * @return
	 */
	public static IMReport delete(IMCustomer customer){
		String sp = "{"+spMap.get(CUST_DEL)+"}";
		
		IMReport ir = jdbcTemplate.execute(sp, new CallableStatementCallback<IMReport>(){
			public IMReport doInCallableStatement(CallableStatement cs)
					throws SQLException, DataAccessException {
				
//				cs.setString(1, customer.getMemberID());
				
				cs.execute();
				
				ResultSet rs = cs.getResultSet();
				
				IMReport ir = new IMReport(); 
				
				while(rs.next()){
					ir.setO_status(rs.getString(1));
					ir.setO_retmsg(rs.getString(2));
					ir.setO_seqno(rs.getString(3));
				}
				
				return ir;
			}
		});
		
		return ir;
	}
	
	/**
	 * 查询会员信息
	 * @param memberID
	 * @param clientID
	 * @return
	 */
	public static IMReport find(String memberID, String clientID){
		String sp = "{"+spMap.get(CUST_QRY)+"}";
		
		IMReport ir = jdbcTemplate.execute(sp, new CallableStatementCallback<IMReport>(){
			public IMReport doInCallableStatement(CallableStatement cs)
					throws SQLException, DataAccessException {
				
//				cs.setString(1, customer.getMemberID());
				
				cs.execute();
				
				ResultSet rs = cs.getResultSet();
				
				IMReport ir = new IMReport(); 
				
				while(rs.next()){
					ir.setO_status(rs.getString(1));
					ir.setO_retmsg(rs.getString(2));
					ir.setO_seqno(rs.getString(3));
				}
				
				return ir;
			}
		});
		
		return ir;
	}
	
	/**
	 * 查询会员复核信息
	 * @param memberID
	 * @return
	 */
	public static IMReport review(String memberID){
		String sp = "{"+spMap.get(CUST_REV)+"}";
		
		IMReport ir = jdbcTemplate.execute(sp, new CallableStatementCallback<IMReport>(){
			public IMReport doInCallableStatement(CallableStatement cs)
					throws SQLException, DataAccessException {
				
//				cs.setString(1, customer.getMemberID());
				
				cs.execute();
				
				ResultSet rs = cs.getResultSet();
				
				IMReport ir = new IMReport(); 
				
				while(rs.next()){
					ir.setO_status(rs.getString(1));
					ir.setO_retmsg(rs.getString(2));
					ir.setO_seqno(rs.getString(3));
				}
				
				return ir;
			}
		});
		
		return ir;
	}
	
}
